package com.idroc.coelacanth.config;

import java.io.InputStream;
import java.util.Properties;

import com.idroc.coelacanth.service.ConfigService;

public class GlobalConfigService implements ConfigService {
	
	private static GlobalConfigService instance = new GlobalConfigService();
	
	public static GlobalConfigService getInstance() {
		return instance;
	}
	
	private Properties props = null;
	
	private GlobalConfigService() {
		
		props = new Properties(System.getProperties());
		InputStream in = null;
		try {
			in = this.getClass().getResourceAsStream("/coelacanth.properties");
			props.load(in);
		} catch (Exception ex) {
			if (null != in) {
				try { in.close(); } catch (Exception iex) {}
			}
			ex.printStackTrace();
		}
	}

	public Object get(String key, Object defaultValue) {

		Object rtv = props.get(key);
		if (null == rtv) {
			rtv = defaultValue;
		}
		return rtv;
	}

	public Object get(String key) {
		
		return props.get(key);
	}

	public Object get(Object key) {
		
		return props.get(key);
	}

	public boolean getBoolean(String key, boolean defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return Boolean.getBoolean(v);
	}

	public boolean getBoolean(String key) {
		
		String v = props.getProperty(key);
		return Boolean.getBoolean(v);
	}

	public char getChar(String key, char defaultValue) {
		
		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return v.charAt(0);
	}

	public char getChar(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return (char)0;
		}
		return v.charAt(0);
	}

	public double getDouble(String key, double defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return Double.parseDouble(v);
	}

	public double getDouble(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return 0;
		}
		return Double.parseDouble(v);
	}

	public int getInt(String key, int defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return Integer.parseInt(v);
	}

	public int getInt(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return 0;
		}
		return Integer.parseInt(v);
	}

	public long getLong(String key, long defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return Long.parseLong(v);
	}

	public long getLong(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return 0;
		}
		return Long.parseLong(v);
	}

	public short getShort(String key, short defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return Short.parseShort(v);
	}

	public short getShort(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return 0;
		}
		return Short.parseShort(v);
	}

	public String getString(String key, String defaultValue) {

		String v = props.getProperty(key);
		if (null == v) {
			return defaultValue;
		}
		return v;
	}

	public String getString(String key) {

		String v = props.getProperty(key);
		if (null == v) {
			return "";
		}
		return v;
	}

}
